package com.htjc.uep.center.basic.service.impl;


import java.math.BigDecimal;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.htjc.uep.center.basic.mapper.BasicApplyMapper;
import com.htjc.uep.center.basic.service.IBasicService;
import com.htjc.uep.center.commons.utils.JsonUtil;
import com.htjc.uep.center.model.basic.request.BasicApplyRequest;
import com.htjc.uep.center.model.basic.response.ApplyResponse;
import com.htjc.uep.center.model.entity.SaBusinessProcess;
import com.htjc.uep.center.model.entity.UpBusinessProcessFlow;
import com.htjc.uep.center.model.entity.UpPool;
import com.htjc.uep.center.model.entity.UpPoolFlow;
import com.htjc.uep.center.model.enums.AcceptStatus;
import com.htjc.uep.center.model.enums.BusinessType;
import com.htjc.uep.center.model.enums.ErrorStatus;
import com.htjc.uep.center.model.enums.PoolStatus;
import com.htjc.uep.center.model.enums.ProcessType;

@Service
public class BasicServiceImpl implements IBasicService{
	
	private Logger log = LoggerFactory.getLogger(this.getClass());
	
	@Autowired
	private BasicApplyMapper applyMapper;

	@Override
//	@Transactional
	public String addPoolApply(BasicApplyRequest applyRequest) {
		log.info("====>>>>积分发行申请业务开始<<<<====");
		
		//是否存在合作平台，如没有则返回异常
		
		if (!applyMapper.queryPlatformIsEmpty(applyRequest.getMechanismCode())) {
			log.info("====>>>>积分发行业务异常<<<<====合作平台不存在,合作平台编号={}", applyRequest.getMechanismCode());
			return JsonUtil.getError(ErrorStatus.ERROR_PALTFORM_NULL);
		}
		
		//生成申请编号
		String applyNo = applyMapper.createApplyNo();
		String operator = applyRequest.getOperator();
		
		//生成下级业务流程
		String downBusiness = insertBusinessProcess(applyRequest, applyNo, BusinessType.APPLY_ADD_POOL_NO.getType(), 
				ProcessType.TYPE_PUBLISH.getCode(), BusinessType.ADD_POOL.getType(), AcceptStatus.ACCEPT_STATUS_02.getCode());
		
		if (downBusiness == null || "".equals(downBusiness)) {
			return JsonUtil.getError(ErrorStatus.ERROR_BUSINESS_NULL);
		} else if (!"1".equals(downBusiness)) {
			//生成下级业务
			insertBusinessProcess(applyRequest, applyNo, BusinessType.APPLY_ADD_POOL_NO.getType(), 
					ProcessType.TYPE_PUBLISH.getCode(), downBusiness, AcceptStatus.ACCEPT_STATUS_03.getCode());
		}
		
		//该合作平台下是否有积分池，如没有将新建积分池
		if (!applyMapper.queryPoolIsEmpty(applyRequest.getRepertoryId(), applyRequest.getMechanismCode())) {
			log.info("====>>>>积分发行业务<<<<====该合作平台下不存在积分池,合作平台编号={},新建积分池开始", applyRequest.getMechanismCode());
			//新建积分池-UP_POOL-积分池编号由外部输入
			UpPool upPool = new UpPool();
			
			upPool.setPOOL_NO(applyRequest.getRepertoryId());
			upPool.setPOOL_NAME(applyRequest.getRepertoryName());
			upPool.setPLATFORM_NO(applyRequest.getMechanismCode());
			upPool.setPOOL_STATUS(PoolStatus.POOL_STATUS_04.getCode());
			upPool.setACCOUNT_NO("");
			//插入数据库
			applyMapper.insertUpPool(upPool);
			log.info("====>>>>积分发行业务<<<<====新建积分池完成,合作平台编号={}", applyRequest.getMechanismCode());
		}
		//新建积分池明细记录-UP_POOL_FLOW-积分池编号由外部输入
		UpPoolFlow poolFlow = new UpPoolFlow();
		
		poolFlow.setAPPLY_NO(applyNo);
		poolFlow.setOPERATOR(operator);
		poolFlow.setPOOL_NO(applyRequest.getRepertoryId());
		poolFlow.setPLATFORM_NO(applyRequest.getMechanismCode());
		poolFlow.setPUBLISH_STATUS(AcceptStatus.ACCEPT_STATUS_01.getCode());
		//200积分1元
		poolFlow.setPUBLISH_TOTAL(applyRequest.getOperatorNum());
		poolFlow.setPUBLISH_AMT(applyRequest.getOperatorNum().divide(new BigDecimal(200)));
		poolFlow.setPUBLISH_STATUS(PoolStatus.POOL_STATUS_04.getCode());
		poolFlow.setREMARK(applyRequest.getRemark());
		//插入数据库
		applyMapper.insertUpPoolFlow(poolFlow);
		
		ApplyResponse response = new ApplyResponse();
		
		response.setBusinessId(applyRequest.getBusinessId());
		response.setAcceptNo(BusinessType.APPLY_ADD_POOL.getType());
		response.setAcceptStatus(AcceptStatus.ACCEPT_STATUS_01.getCode());
		
		return JsonUtil.getSuccess(response);
	}

	@Override
	public String addPlatform(BasicApplyRequest applyRequest) {
		log.info("====>>>>合作平台申请业务开始<<<<====");
		
		return null;
	}
	
	private String insertBusinessProcess(BasicApplyRequest applyRequest, String applyNo, String processNo, String processType, 
			String businessNo, String processStatus) {
		
		//查询流程
		SaBusinessProcess businessProcess = applyMapper.queryBusinessProcess(processNo);
		
		if (businessProcess == null) {
			log.info("====>>>>积分申请业务<<<<====业务流程不存在,流程编号={}", processNo);
			throw null;
		}
		
		String[] processTree = businessProcess.getPROCESS_TREE().split("\\|");
		
		UpBusinessProcessFlow businessFlow = new UpBusinessProcessFlow();
		businessFlow.setAPPLY_NO(applyNo);
		businessFlow.setOPERATOR(applyRequest.getOperator());
		businessFlow.setPLATFORM_NO(applyRequest.getMechanismCode());
		businessFlow.setPROCESS_EXPLAIN(applyRequest.getOperatorNum());
		businessFlow.setPROCESS_NO(processNo);
		businessFlow.setPROCESS_STATUS(processStatus);
		businessFlow.setREMARK(applyRequest.getRemark());
		
		for (int i = 0; i < processTree.length; i++) {
			if (processTree[i].equals(businessNo)) {
				if (i - 1 >= 0) {
					//上一级
					businessFlow.setUP_BUSINESS(processTree[i-1]);
				} else {
					//顶级
					businessFlow.setUP_BUSINESS("0");
				}
				businessFlow.setSAME_BUSINESS(processTree[i]);
				if (i + 1 < processTree.length) {
					businessFlow.setDOWN_BUSINESS(processTree[i+1]);
				} else {
					businessFlow.setDOWN_BUSINESS("1");
				}
				break;
			}
		}
		
		applyMapper.insertUpProcessFlow(businessFlow);
		return businessFlow.getDOWN_BUSINESS();
	}
}














































